package cn.lgwen.candy.runtime.operator;

/**
 * 2019/12/26
 * aven.wu
 * danxieai258@163.com
 */
public class Fetcher<T> {

    private T next;

    private final Object lock = new Object();

    public T pollNext() {
        synchronized (lock) {
            while (next == null) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }            }
            T n = next;
            next = null;
            lock.notifyAll();
            return n;
        }
    }

    public void produce(T t) {
        synchronized (lock) {
            while (next != null) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            next = t;
            lock.notifyAll();
        }
    }

}
